package com.tjhk.netty.test.model;


import com.tjhk.netty.model.AbstractMessage;
import com.tjhk.netty.serialize.DataType;
import com.tjhk.netty.serialize.annotation.Field;
import com.tjhk.netty.util.ByteBufUtils;
import io.netty.buffer.ByteBuf;

import java.time.LocalDateTime;
import java.util.Arrays;

public class MyMessage extends AbstractMessage {
    private Object header;
    private byte[] payload;

    public MyMessage(int messageId, Object header) {
        this.setMessageId(messageId);
        this.header = header;
    }

    public Object getHeader() {
        return this.header;
    }

    @Override
    public void setPayload(ByteBuf bodyBuf) {
        this.payload = ByteBufUtils.readFixLength(bodyBuf,bodyBuf.readableBytes());
    }

    @Override
    public byte[] getPayload() {
        return this.payload;
    }

    public static class Header{
        public static int headerLength = 3;

        @Field(index = 0,length = 1,type = DataType.BYTE,desc = "消息类型")
        private Integer messageId;

        @Field(index = 0,length = 2,type = DataType.WORD,desc = "数据长度")
        private Integer bodyLength;

        public void setMessageId(Integer messageId) {
            this.messageId = messageId;
        }

        public Integer getMessageId() {
            return this.messageId;
        }

        public Integer getBodyLength() {
            return bodyLength;
        }

        public void setBodyLength(Integer bodyLength) {
            this.bodyLength = bodyLength;
        }
    }


    private String body;

    @Field(index = 0,length = 1,type = DataType.BYTE,desc = "年龄")
    private Integer age;

    @Field(index = 1,length = 6,type = DataType.BCD8421,desc = "出生年月")
    private LocalDateTime birthday;

    @Field(index = 7,length = 4,type = DataType.STRING,desc = "姓名")
    private String name;

    @Field(index = 11,length = 8,type = DataType.BYTES,desc = "需要自己转换的字段")
    private byte[] dynamic;

    @Field(index = 19,length = 10,type = DataType.BCD8421,desc = "终端手机号")
    private String mobile;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public byte[] getDynamic() {
        return dynamic;
    }

    public void setDynamic(byte[] dynamic) {
        this.dynamic = dynamic;
    }

    public LocalDateTime getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDateTime birthday) {
        this.birthday = birthday;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getBody() {
        return body;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    public String toString() {
        return "MyMessage{" +
                "messageId=" + getMessageId() +
                ", body='" + body + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", name='" + name + '\'' +
                ", dynamic=" + Arrays.toString(dynamic) +
                '}';
    }
}